package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiantian.dao.OrderWhisperInformationRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Orderwhisperinformation;
import com.tiantian.domain.OrderwhisperinformationId;
import com.tiantian.service.OrderWhisperInformationService;

/**
 * @description 审核管理
 */
@Service
@Transactional
public class OrderWhisperInformationServiceImpl extends GenericServiceImpl<Orderwhisperinformation, OrderwhisperinformationId> implements
		OrderWhisperInformationService {
	private OrderWhisperInformationRepository repository;

	@Autowired
	public void setRepository(OrderWhisperInformationRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	public List<Map<String, Object>> getCompaintsInfo(Map<String, Object> map) {
		String sql = "select * from OrderWhisperInformation a";
		sql += " where exists (";
		// sql +=
		// " select 1 from OrderWhisperRelationship b where a.OrderWhisperInfoID=b.WhisperRelationsID and orderid=:orderId and userid1=:userId1 and userid2=:userId2 )";
		sql += " select 1 from OrderWhisperRelationship b where a.OrderWhisperInfoID=b.WhisperRelationsID and orderid=:orderId  )";

		sql += " order by sentime desc";
		Map<String, Object> param = new HashMap<>();
		param.put("orderId", map.get("orderId"));
		// param.put("userId1", map.get("userId1"));
		// param.put("userId2", map.get("userId2"));
		return this.repository.queryHumpListMapBySql(sql, param);
	}

	public String getDrumNo(String sendTime, String OrderWhisperInfoID) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		sql.append("select max(drumno) drumno from OrderWhisperInformation  ");
		sql.append("  where 1=1 ");
		if (StringUtils.isNotBlank(sendTime)) {
			sql.append(" and DATE_FORMAT(sentime,'%Y-%m-%d')=:sendTime");
			params.put("sendTime", sendTime);
		}
		if (StringUtils.isNotBlank(OrderWhisperInfoID)) {
			sql.append(" and OrderWhisperInfoID=:OrderWhisperInfoID");
			params.put("OrderWhisperInfoID", OrderWhisperInfoID);
		}
		List<Map<String, Object>> ret = repository.queryHumpListMapBySql(sql.toString(), params);
		if (ret != null && ret.size() > 0) {
			if (ret.get(0).get("drumno") != null) {
				return ret.get(0).get("drumno") + "";
			}
		}
		return null;
	}

}
